package net.noday.base.fanli;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) throws IOException, InterruptedException {
    	try {
			String queryUrl = "http://xiaozu.fanli.com/group/ajaxGetTopicReplyInfo";
			Integer floor = 0;
			int currentId = 1;
			int i=10;
			boolean send = false;
			int sleep = 1000;
			String tid = "78979";
			while (true) {
				Document doc;
				try {
					doc = Jsoup.connect(queryUrl)
//    				.cookie("51fanli_login_username", "at1943@163.com")
//    				.cookie("prouserid", "40831373")
//    				.cookie("prousername", "at1943")
//    				.cookie("prousernameutf", "at1943")
//    				.cookie("loginverify", "b92cce040c8aca46")
//    				.cookie("prolong", "1419817161")
//    				.cookie("PHPSESSID", "muryfg6wcz7aql3qybxwecw8wl")
							.data("tid", tid)
							.data("gid", "18")
							.data("current_id", String.valueOf(currentId))
//		    		.data("floor", String.valueOf(floor))
							.data("bottom", "1")
							.post();
				} catch (Exception e) {
					return;
				}
				JSONObject resp = JSON.parseObject(doc.body().text());
				if (resp.getIntValue("status") == 1) {
					JSONObject data = resp.getJSONObject("data");
					int count = data.getIntValue("page_count");
					System.out.println(floor+":"+count);
					if ((send && count >1) || (!send && count>0)) {
						currentId = data.getIntValue("current_id");
						floor=floor+count;
						if ((floor < 30)||((floor > 290)&&(floor < 350))||((floor > 430)&&(floor < 500))||((floor > 590)&&(floor < 650))||((floor > 790)&&(floor < 830))) {
							send = true;
							Thread t = new Thread(new AddReplyTopic(i, tid));
							t.start();
							i++;
						}
					}
					
				}
				Thread.sleep(sleep*(floor/30));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
//    	int lastFloor = 0;
//    	int sleepTime = 1000;
//    	for (int i = 0; i <msgArr.length; i++) {
//			
//    		Document doc = Jsoup.connect("http://xiaozu.fanli.com/group/ajaxAddReplyTopic")
//    				.cookie("51fanli_login_username", "at1943@163.com")
//    				.cookie("prouserid", "40831373")
//    				.cookie("prousername", "at1943")
//    				.cookie("prousernameutf", "at1943")
//    				.cookie("loginverify", "b92cce040c8aca46")
//    				.cookie("prolong", "1419817161")
//    				.cookie("PHPSESSID", "muryfg6wcz7aql3qybxwecw8wl")
//		    		.data("content", msgArr[i])
//		    		.data("tid", "66803")
//		    		.data("gid", "18")
//		    		.post();
//    		System.out.println("发消息["+i+"]"+msgArr[i]);
//    		System.out.println("响应"+doc.body().text() + i);
//    		JSONObject resp = JSON.parseObject(doc.body().text());
//    		int floor = resp.getJSONObject("data").getIntValue("floor");
//    		if ((lastFloor+1)==floor) {
//				
//			}
//    		lastFloor = floor;
//    		Thread.sleep(60000);
//		}
    }
}
